package com.zhenai.rocyuan.modules.rbac.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcOperations;

import com.alibaba.fastjson.JSON;
import com.zhenai.rocyuan.commons.uitls.FixCommonUtils;
import com.zhenai.rocyuan.modules.rbac.models.Ar;
import com.zhenai.rocyuan.modules.rbac.models.UriCache;
import com.zhenai.rocyuan.modules.rbac.service.MemcachedService;
import com.zhenai.rocyuan.modules.rbac.service.RbacService;

public class RbacServiceImpl implements RbacService {
	private SimpleJdbcOperations crmBJdbc;
	private MemcachedService memcachedService;
	private int exp;
	private static final String link = "_";

	@Override
	public boolean hasUri(Integer currentWorkerId, String uri) {
		String key = uri + link + currentWorkerId;
		UriCache uc = getUriCache(key);
		if(uc != null) {
			if(uc.getH() != null) {
				return uc.getH();
			}
		} else {
			uc = new UriCache(); 
		}
		String sql = "select count(1) from WorkerOfRole wor join ActionOfRole aor on aor.roleId=wor.roleId join " +
				"Action a on a.actionId=aor.actionId where wor.workerId=? and a.actionUri=?";
		boolean result = crmBJdbc.queryForInt(sql, currentWorkerId, uri) > 0;
		uc.setH(result);
		memcachedService.set(key, exp, JSON.toJSONString(uc));
		return result;
	}

	@Override
	public boolean hasUri(Integer currentWorkerId, String uri, int typeId, int[] rangeValue) {
		String key = uri + link + currentWorkerId;
		UriCache uc = getUriCache(key);
		if (uc != null) {
			List<Ar> ar = uc.getAr();
			if (ar != null) {
				for (Ar ar2 : ar) {
					if(ar2.getT()==typeId) {
						for (int r : rangeValue) {
							if(ar2.getV() == r) {
								return true;
							}
						}
					}
				}
				return false;
			}
		} else {
			uc = new UriCache();
		}
		boolean result = false;
		List<Ar> list = getActionRange(currentWorkerId, uri);
		if(FixCommonUtils.isNotBlank(list)) {
			for (Ar ar2 : list) {
				if(ar2.getT()==typeId) {
					if(result)	break;
					for (int r : rangeValue) {
						if(ar2.getV() == r) {
							result = true;
							break;
						}
					}
				}
			}
		}
		uc.addAr(list);
		memcachedService.set(key, exp, JSON.toJSONString(uc));
		return result;
	}

	@Override
	public int getRangeId(Integer currentWorkerId, String uri) {
		String key = uri + link + currentWorkerId;
		UriCache uc = getUriCache(key);
		int temp = -1000 , result = -1;
		if(uc != null) {
			List<Ar> ar = uc.getAr();
			if (ar != null) {
				for (Ar ar2 : ar) {
					if(temp < ar2.getS()) {
						temp = ar2.getS();
						result = ar2.getId();
					}
				}
				return result;
			}
		} else {
			uc = new UriCache(); 
		}
		List<Ar> list = getActionRange(currentWorkerId, uri);
		if(FixCommonUtils.isNotBlank(list)) {
			for (Ar ar : list) {
				if(temp < ar.getS()) {
					temp = ar.getS();
					result = ar.getId();
				}
			}
		}
		uc.addAr(list);
		memcachedService.set(key, exp, JSON.toJSONString(uc));
		return result;
	}

	@Override
	public int getValueByType(Integer currentWorkerId, String uri, int type) {
		String key = uri + link + currentWorkerId;
		UriCache uc = getUriCache(key);
		int temp = -1000 , result = -1;
		if(uc != null) {
			List<Ar> ar = uc.getAr();
			if (ar != null) {
				for (Ar ar2 : ar) {
					if(temp < ar2.getS() && type == ar2.getT()) {
						temp = ar2.getS();
						result = ar2.getV();
					}
				}
				return result;
			}
		} else {
			uc = new UriCache(); 
		}
		List<Ar> list = getActionRange(currentWorkerId, uri);
		if(FixCommonUtils.isNotBlank(list)) {
			for (Ar ar : list) {
				if(temp < ar.getS() && type == ar.getT()) {
					temp = ar.getS();
					result = ar.getV();
				}
			}
		}
		uc.addAr(list);
		memcachedService.set(key, exp, JSON.toJSONString(uc));
		return result;
	}
	
	@Override
	public List<Integer> getValuesByType(Integer currentWorkerId, String uri, int type) {
		String key = uri + link + currentWorkerId;
		UriCache uc = getUriCache(key);
		List<Integer> result = new ArrayList<Integer>();
		if(uc != null) {
			List<Ar> ar = uc.getAr();
			if (ar != null) {
				for (Ar ar2 : ar) {
					if(type == ar2.getT()) {
						result.add(ar2.getV());
					}
				}
				return result;
			}
		} else {
			uc = new UriCache(); 
		}
		List<Ar> list = getActionRange(currentWorkerId, uri);
		if(FixCommonUtils.isNotBlank(list)) {
			for (Ar ar : list) {
				if(type == ar.getT()) {
					result.add(ar.getV());
				}
			}
		}
		uc.addAr(list);
		memcachedService.set(key, exp, JSON.toJSONString(uc));
		return result;
	}
	
	@Override
	public int getRangeValue(Integer currentWorkerId, String uri) {
		String key = uri + link + currentWorkerId;
		UriCache uc = getUriCache(key);
		int temp = -1000 , result = -1;
		if(uc != null) {
			List<Ar> ar = uc.getAr();
			if (ar != null) {
				for (Ar ar2 : ar) {
					if(temp < ar2.getS()) {
						temp = ar2.getS();
						result = ar2.getV();
					}
				}
				return result;
			}
		} else {
			uc = new UriCache(); 
		}
		List<Ar> list = getActionRange(currentWorkerId, uri);
		if(FixCommonUtils.isNotBlank(list)) {
			for (Ar ar : list) {
				if(temp < ar.getS()) {
					temp = ar.getS();
					result = ar.getV();
				}
			}
		}
		uc.addAr(list);
		memcachedService.set(key, exp, JSON.toJSONString(uc));
		return result;
	}

	@Override
	public List<Ar> getActionRange(Integer currentWorkerId, String uri) {
		String sql = "select distinct ar.rangeId id,ar.rangeValue v,ar.typeId t,ar.orderNo s from WorkerOfRole wor join ActionOfRole aor on aor.roleId=wor.roleId join Action a on a.actionId=aor.actionId "
				+ "join ActionRange ar on ar.rangeId=aor.rangeId where wor.workerId=? and a.actionUri=?";
		return crmBJdbc.query(sql,ParameterizedBeanPropertyRowMapper.newInstance(Ar.class),currentWorkerId, uri);
	}
	
	public UriCache getUriCache(String key) {
		String value = memcachedService.get(key);
		if(StringUtils.isBlank(value))	return null;
		return JSON.parseObject(value, UriCache.class);
	}
	
	public void setCrmBJdbc(SimpleJdbcOperations crmBJdbc) {
		this.crmBJdbc = crmBJdbc;
	}

	public void setMemcachedService(MemcachedService memcachedService) {
		this.memcachedService = memcachedService;
	}

	public void setExp(int exp) {
		this.exp = exp;
	}
	public static void main(String[] args) {
//		List<Integer> list = JSONArray.parseArray(null, Integer.class);
//		System.out.println(JSONArray.toJSONString(null));
//		System.out.println(list);
		
	}
}
